<?php
/**
 * Считалка посетителей.
 *
 * Объекты можно посещать.
 * Посещать могут объекты.
 * Интересует время визита.
 *
 * Этот мир определяется триадой: (who_oid, what_oid, datetime)
 * где who_oid -- кто посетил
 *    what_oid -- кого/что посетил
 *    datetime -- когда посетил
 *
 * @package default
 * @author lucky
 **/
$this->useClass('RL_Model2');

class Model_Visit extends RL_Model2
{
	var $tbl_visit = 'visit';

	public function visit ($who, $what, $ts=NULL)
	{
		if (!isset($who)) return false;
		if (!isset($what)) return false;

		if (!isset($ts)) $ts = time();

		$ks = array('y', 'm', 'd');
		$vs = explode(' ', date('Y m d', $ts));
		$v = array_combine($ks, $vs);
		$v['who_oid'] = is_scalar($who) ? $who : $who->id;
		$v['what_oid'] = is_scalar($what) ? $what : $what->id;
		$v['ts'] = $ts;

		$db = $this->getDb();
		$prefix = $db->db_prefix;

		$t = array_map(array($db,'quote'), $v);

		$sql = " SELECT 1 FROM {$prefix}{$this->tbl_visit}".
					 " WHERE who_oid={$t['who_oid']} AND what_oid={$t['what_oid']}".
					 "   AND y={$t['y']} AND m={$t['m']} AND d={$t['d']}".
					 " LIMIT 1";
		$r = $db->queryOne($sql);
		if ($r) {
				$db->execute(
					" UPDATE {$prefix}{$this->tbl_visit}".
					" SET ts={$t['ts']},".
					"     visit_count=visit_count+1".
					" WHERE who_oid={$t['who_oid']} AND what_oid={$t['what_oid']}".
					"   AND y={$t['y']} AND m={$t['m']} AND d={$t['d']}");
		} else {
			$sql = " INSERT INTO {$prefix}{$this->tbl_visit} (who_oid, what_oid, y, m, d, ts, visit_count)".
				     " VALUES ({$t['who_oid']},{$t['what_oid']},{$t['y']},{$t['m']},{$t['d']},{$t['ts']},1)";
			$db->insert($sql);
		}
	} // end of method visit

	public function getDailyItemsInfo ($qf)
	{
		$db = $this->getDb();
		$prefix = $db->db_prefix;

		$fields = array('what_oid', 'visit_count', 'COUNT(DISTINCT who_oid) AS visitor_count', 'MAX(ts) AS last_ts');
		$from = array("{$prefix}{$this->tbl_visit}");
		$where = array();
		$group_by = array('what_oid', 'y', 'm', 'd');
		$order_by = array('y desc', 'm desc', 'd desc', 'visitor_count desc', 'visit_count desc', 'last_ts desc');


		if (isset($qf['community'])) {
			$what = $qf['community']->getWhat();
			$community_oid = is_scalar($what) ? $what : $what->id;
			//$sql .= "INNER JOIN {$prefix}class_allfeed AF ON what_oid = oid AND AF.community_oid = {$community_oid}";
			$row = $db->query("SELECT lft, rgt FROM t_acl_tree WHERE id_object = {$community_oid}");
			$lft = $row[0]['lft']; $rgt = $row[0]['rgt'];

			$from[] = "INNER JOIN t_acl_tree AT ON what_oid = AT.id_object AND AT.lft >= {$lft} AND AT.rgt <= {$rgt}";
			$fields[] = 'AT.lft';
			//array_unshift($order_by, 'lft');
		}

		$sql = "SELECT ".implode(', ', $fields)
			.   " FROM ".implode(" ", $from).($where ?
			    " WHERE ".implode(" AND ", $where):'').($group_by ?
			    " GROUP BY ".implode(", ", $group_by):'').($order_by ?
			    " ORDER BY ".implode(", ", $order_by):'');
		return $db->query($sql);
	} // end of method getDailyVisitsInfo

	private function getDb ()
	{
		return $this->store->db;
	} // end of method getDb
	private function getStore ()
	{
		return $this->store;
	} // end of method getDb

} // END class
